<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>在字符串所有新行之前插入 HTML 换行标记</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.nl-langinfo.html">nl_langinfo</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.number-format.html">number_format</a></div>
 <div class="up"><a href="ref.strings.html">字符串函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.nl2br" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">nl2br</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">nl2br</span> &mdash; <span class="dc-title">在字符串所有新行之前插入 HTML 换行标记</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.nl2br-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>nl2br</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$is_xhtml</code><span class="initializer"> = true</span></span>
  ] )</div>

  <p class="para rdfs-comment">
   在字符串 <em><code class="parameter">string</code></em> 所有新行之前插入 &#039;&lt;br /&gt;&#039; 或 &#039;&lt;br&gt;&#039;，并返回。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.nl2br-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">string</code></em></dt>

     <dd>

      <p class="para">
       输入字符串。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">is_xhtml</code></em></dt>

     <dd>

      <p class="para">
       是否使用 XHTML 兼容换行符。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.nl2br-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回调整后的字符串。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.nl2br-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4753">
    <p><strong>Example #1 <span class="function"><strong>nl2br()</strong></span> 使用范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">nl2br</span><span style="color: #007700">(</span><span style="color: #DD0000">"foo&nbsp;isn't\n&nbsp;bar"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
foo isn&#039;t&lt;br /&gt;
 bar
</pre></div>
    </div>
   </div>
   <div class="example" id="example-4754">
    <p><strong>Example #2 使用 <em><code class="parameter">is_xhtml</code></em> 生成合法的 HTML 标记</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">nl2br</span><span style="color: #007700">(</span><span style="color: #DD0000">"Welcome\r\nThis&nbsp;is&nbsp;my&nbsp;HTML&nbsp;document"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Welcome&lt;br&gt;
This is my HTML document
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.nl2br-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
         新增可选的 <em><code class="parameter">is_xhtml</code></em> 参数。
       </td>
      </tr>

      <tr>
       <td>4.0.5</td>
       <td>
        <span class="function"><strong>nl2br()</strong></span> 成为 XHTML 兼容的操作。所有的旧版本都会在 <em><code class="parameter">string</code></em> 的新行之前插入 &#039;&lt;br&gt;&#039; 而不是 &#039;&lt;br /&gt;&#039;。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.nl2br-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.htmlspecialchars.html" class="function" rel="rdfs-seeAlso">htmlspecialchars()</a> - Convert special characters to HTML entities</span></li>
    <li class="member"><span class="function"><a href="function.htmlentities.html" class="function" rel="rdfs-seeAlso">htmlentities()</a> - Convert all applicable characters to HTML entities</span></li>
    <li class="member"><span class="function"><a href="function.wordwrap.html" class="function" rel="rdfs-seeAlso">wordwrap()</a> - 打断字符串为指定数量的字串</span></li>
    <li class="member"><span class="function"><a href="function.str-replace.html" class="function" rel="rdfs-seeAlso">str_replace()</a> - 子字符串替换</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.nl-langinfo.html">nl_langinfo</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.number-format.html">number_format</a></div>
 <div class="up"><a href="ref.strings.html">字符串函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
